package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.MenuDao;
import com.aaa.qy127.entity.Menu;
import com.aaa.qy127.entity.Tree;
import com.aaa.qy127.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository("menuDaoImpl")
public class MenuDaoImpl implements MenuDao {

    /**
     * 查询所有的 父菜单 和子菜单 的信息
     * @param roleid
     * @return
     */
    @Override
    public List<Menu> getAllMenuByRid(Integer roleid) {
        String sql ="select DISTINCT tm.mid,tm.mname,tm.mcode,tm.murl,tm.mpid,tm.micon from t_menu tm INNER JOIN t_role_premission trp ON tm.mid = trp.mid where trp.rid = ? AND tm.mpid = 0";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, roleid);//此处是获取到父菜单的所有信息

        if (maps.size() >0){
            //定义一个大的ListMenu存放父级 和子级 双层菜单信息
            List<Menu> menus = new ArrayList<>();
            for (Map<String, Object> map : maps) {
                //此处sonMenu先置null，下边查询过子菜单数据后再填充
                Menu menu = new Menu((Integer) map.get("mid"),(String)map.get("mname"),(String)map.get("murl"),null);

                //2、根据父菜单的mid查询子菜单信息
                String sonSql = "select DISTINCT ts.mid,ts.mname,ts.mcode,ts.murl,ts.mpid,ts.micon from t_menu tf INNER JOIN t_menu ts ON tf.mid  = ts.mpid INNER JOIN t_role_premission trp ON trp.mid = ts.mid where tf.mid = ? and trp.rid = ?";
                //此处根据父菜单 mid 获取子菜单 信息
                List<Map<String, Object>> sonMap = JDBCUtil.queryMap(sonSql, map.get("mid"),roleid);
                //判断子菜单中是否有值
                //定义一个 一个小的ListMenu 存放子菜单的信息
                List<Menu> sonMenu = new ArrayList<>();
                if (sonMap.size()>0){
                    for (Map<String, Object> map1 : sonMap) {
                        Menu menu1 = new Menu((Integer) map1.get("mid"),(String)map1.get("mname"),(String)map1.get("murl"));
                        sonMenu.add(menu1);
                        //System.out.println(menu1);
                    }
                    menu.setSonMenu(sonMenu);
                    menus.add(menu);
                }else {
                    menus.add(menu);
                }
            }
            return menus;
        }
        return null;
    }

    /**
     * 获取所有权限菜单
     * @return
     */
    @Override
    public List<Tree> getAllMenu() {
        String sql = "select  mid,mname from t_menu WHERE mpid =0";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql);
        //创建一个 大的ListTree 存放父菜单 信息
        List<Tree> trees = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Tree tree = new Tree((Integer) map.get("mid"),(String) map.get("mname"),null);

            //根据父菜单的mid 查询子菜单的信息
            String  sql1= "select t1.mid,t1.mname from t_menu t INNER JOIN t_menu t1 ON t.mid = t1.mpid where t.mid = ?";
           //此处根据父菜单mid获取子菜单信息
            List<Map<String, Object>> maps1 = JDBCUtil.queryMap(sql1, map.get("mid"));
            List<Tree> sonTree = new ArrayList<>();
            //对得到的子菜单遍历 将数据转存到ListTree中
            for (Map<String, Object> map1 : maps1) {
                Tree tree1 = new Tree((Integer) map1.get("mid"),(String)map1.get("mname"),null);
                sonTree.add(tree1);
            }
            tree.setChildren(sonTree);
            trees.add(tree);
        }
        return trees;
    }

    /**
     * 根据rid 获取到 这个角色的所有的 子菜单的mid
     * @param rid
     * @return
     */
    @Override
    public List<Map<String, Object>> getAllMidByRid(String rid) {
        String sql ="select trp.mid from t_role_premission trp INNER JOIN t_menu tm ON trp.mid = tm.mid and tm.mpid != 0 where trp.rid = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, rid);
        return maps;
    }

    /**
     * 将权限和角色中间表中的所有跟当前rid对应的记录都删除
     * @param rid
     */
    @Override
    public void delRolePremissionByRid(String rid) {
        String sql = "delete from t_role_premission where rid =?";
        JDBCUtil.update(sql, rid);
    }

    /**
     * 将传过来的数组中的所有mid和rid，再重新插入到权限和角色中间表中
     * @param rid
     * @param split
     */
    @Override
    public void insertRolePremission(String rid, String[] split) {
        for (String s : split) {
            String sql = "insert into t_role_premission values(null,?,?)";
            JDBCUtil.update(sql, rid, s);
        }

    }
}
